package cn.itcast.bos.web.action;


import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.core.MediaType;

import org.apache.commons.io.FileUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.bos.constant.Constants;
import cn.itcast.bos.domain.page.PageBean;
import cn.itcast.bos.domain.take_delivery.Promotion;
import cn.itcast.bos.web.action.common.BaseAction;
import freemarker.template.Configuration;
import freemarker.template.Template;

@ParentPackage(value="json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class PromotionAction extends BaseAction<Promotion>{
	private Integer page;
	// 当前页显示的记录数
	private Integer pageSize;
	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}


	public void setPage(Integer page) {
		this.page = page;
	}


	/**
	 * 前台分页查询
	 * @return
	 * @throws Exception
	 */
	@Action(value = "promotion_pageQuery",results={@Result(name="success",type="json")})
	public String  pageQuery() {
		//调用webService ,从bos_management 调取前台信息
		PageBean<Promotion> pageData = WebClient
		.create(Constants.BOS_MANAGEMENT_URL+"/services/promotionService/findPageData?page="+page+"&rows="+pageSize)
		.accept(MediaType.APPLICATION_JSON)
		.get(PageBean.class);
		//返回json
		Map<String , Object> map = new  HashMap<String , Object>();
		map.put("totalCount", pageData.getTotalElements());
		map.put("pageData", pageData.getContent());
		ServletActionContext.getContext().getValueStack().push(map);
		return SUCCESS;
		
	}
	
	/**
	 * 查看活动详情
	 * @return
	 * @throws Exception
	 */
	@Action(value="promotion_showDetail")
	public String showDetail() throws Exception {
		// 先判断id，是否对应html，是否存在，如果存在，直接返回
		// 查找路径中文件
		String htmlRealPath = ServletActionContext.getServletContext().getRealPath("/freemarker");
		File file = new File(htmlRealPath+"/"+model.getId()+".html");
		
		//如果文件不存在,查询数据库
		if (!file.exists()) {
			Configuration configuration = new Configuration(Configuration.VERSION_2_3_22);
			configuration.setDirectoryForTemplateLoading(new File(ServletActionContext.getServletContext().getRealPath("/WEB-INF/template_freemarker")));
			//获取模板对象
			Template template = 	configuration.getTemplate("promotion.ftl");
			//动态数据获取
			Promotion promotion = WebClient
					.create(Constants.BOS_MANAGEMENT_URL+"/services/promotionService/findById/"
					 + model.getId()).accept(MediaType.APPLICATION_JSON).get(Promotion.class);
			
			//使用map集合创建动态数据对象 
			Map<String, Object> parameterMap = new HashMap<String,Object>();
			parameterMap.put("promotion", promotion);
			template.process(parameterMap, new OutputStreamWriter(new FileOutputStream(file),"utf-8"));
		}
		
		// 如果html文件存在，直接将文件返回，响应编码用UTF-8的形式
			ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
			FileUtils.copyFile(file, ServletActionContext.getResponse().getOutputStream());
	
		return NONE;
	}
		
		
		
}









